<template>
  <div class="page-main" v-title data-title="领用物料列表">
    <home-nav :userData="userinfo"></home-nav>
    <div class="banner"><span>领用物料列表</span></div>
    <div class="container">
      <div class="header">
        <a-row type="flex" justify="space-between" align="middle">
          <a-col style="display: flex; justify-content: start">
            <a-button class="hte-btn del-btn" @click="handleSearch">刷新</a-button>

            <a-button class="hte-btn del-btn" @click="handleExport">导出</a-button>
          </a-col>
          <a-col style="display: flex; justify-content: end">
            <a-input-search
              v-model="searchForm.keyword"
              @search="handleSearch"
              :allowClear="true"
              placeholder="请按照关键字进行搜索"
            />
            <a-button class="hte-btn del-btn" @click="showSearchForm = true">高级搜索</a-button>
          </a-col>
        </a-row>
      </div>

      <a-modal
        :visible="showSearchForm"
        width="70vw"
        :bodyStyle="{ padding: '10px' }"
        title="高级查询"
        @ok="handleSearch"
        @cancel="showSearchForm = false"
      >
        <a-form-model
          ref="ruleForm"
          :laba-col="labelCol"
          :wrapper-col="wrapperCol"
          @keyup.enter.native="handleToSearch"
        >
          <a-row :gutter="24">
            <a-col :span="8">
              <a-form-model-item label="申领状态:" prop="materialsType">
                <a-select allowClear v-model="searchForm.applyStatus" placeholder="申领状态">
                  <a-select-option value=""> 全部 </a-select-option>
                  <template v-for="item in codes.MaterialsApplyStatus">
                    <a-select-option :key="item.code" :value="item.code">
                      {{ item.name }}
                    </a-select-option>
                  </template>
                </a-select>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="申领单号:" prop="materialsType">
                <a-input v-model="searchForm.consumNum" allowClear> </a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="费用类型:" prop="costNum">
                <a-select allowClear v-model="searchForm.costType" placeholder="费用类型">
                  <template v-for="item in costTypeOptions">
                    <a-select-option :key="item.code" :value="item.code">
                      {{ item.text }}
                    </a-select-option>
                  </template>
                </a-select>
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row :gutter="24">
            <a-col :span="8">
              <a-form-model-item label="项目名称:">
                <a-input v-model="searchForm.projectName" allowClear> </a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="项目经理:">
                <a-input v-model="searchForm.projectManager" allowClear> </a-input>
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="费用号:">
                <a-input v-model="searchForm.costNum" allowClear> </a-input>
              </a-form-model-item>
            </a-col>
          </a-row>
          <a-row :gutter="24">
            <a-col :span="8">
              <a-form-model-item label="物料号:">
                <a-input v-model="searchForm.matnr" allowClear />
              </a-form-model-item>
            </a-col>
            <a-col :span="8">
              <a-form-model-item label="物料描述:">
                <a-input v-model="searchForm.maktx" allowClear />
              </a-form-model-item>
            </a-col>
            <!-- <a-col :span="8">
              <a-form-model-item label="审批人:">
                <a-input v-model="searchForm.auditUser" allowClear />
              </a-form-model-item>
            </a-col> -->
          </a-row>
        </a-form-model>
      </a-modal>
      <div class="content-list">
        <a-table
          :scroll="{ x: '100%' }"
          class="hte-table-content"
          :columns="columns"
          :data-source="records"
          rowKey="randomStr"
          bordered
          :pagination="false"
          :customRow="customRow"
          size="small"
          :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        >
          <template slot="statusRender" slot-scope="text, record">
            <template v-for="(code, index) in codes.MaterialsApplyStatus">
              <span :key="index" v-if="record.applyStatus == code.code"> {{ code.name }} </span>
            </template>
          </template>
          <template slot="consumNumRender" slot-scope="text, record">
            <span class="font-active" @click="toDetail(record.id)"> {{ record.consumNum }} </span>
          </template>
        </a-table>
        <a-pagination
          show-size-changer
          :default-current="1"
          :total="searchForm.total"
          @change="pageChange"
          @showSizeChange="onShowSizeChange"
          :show-total="(total) => `共 ${total} 条记录`"
        />
      </div>
    </div>
  </div>
</template>
<script>
import { postAction } from '@/api/manage'
import { CodesMixin } from '@/mixins/CodesMixin'
import { UtilsMixin } from '@/mixins/UtilsMixin'
export default {
  mixins: [CodesMixin,UtilsMixin],
  components: {
    HomeNav: () => import('@/components/layouts/HomeNav'),
  },
  props: {},
  mounted() {},
  data() {
    return {
      userinfo: {
        info: this.$store.getters.userInfo,
        module: 'ContractTaskList',
      },
      showSearchForm: false,
      labelCol: { span: 8 },
      wrapperCol: { span: 15 },
      label: '费用号',
      records: [],
      searchForm: {
        offset: 0,
        total: 0,
        current: 1,
        pageSize: 20,
        pageSizeOptions: ['10', '20', '30', '40', '50'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
      },
      costTypeOptions: [
        { code: '1', text: '成本中心' },
        { code: '2', text: 'WBS号' },
      ],
      materialsTypeOptions: [
        { code: '1', text: '预留' },
        { code: '2', text: 'WBS' },
        { code: '3', text: '公有' },
      ],
      columns: [
        {
          title: '申领状态',
          dataIndex: 'applyStatus',
          width: '120px',
          align: 'center',
          scopedSlots: { customRender: 'statusRender' },
        },
        {
          title: '申领单号',
          dataIndex: 'consumNum',
          width: '120px',
          align: 'center',
          scopedSlots: { customRender: 'consumNumRender' },
        },
        {
          title: '物料类型',
          dataIndex: 'materialsType',
          width: '100px',
          align: 'center',
          // scopedSlots: { customRender: 'materialsTypeRender' },
        },
        {
          title: '寄售标志',
          dataIndex: 'sobkz',
          width: '100px',
          align: 'center',
          scopedSlots: { customRender: 'sobkzRender' },
        },
        {
          title: '拣配单号',
          dataIndex: 'jpno',
          width: '140px',
          align: 'center',
        },
        {
          title: '配送单号',
          dataIndex: 'psno',
          width: '140px',
          align: 'center',
        },
        {
          title: '物料编码',
          dataIndex: 'matnr',
          width: '120px',
          align: 'center',
          scopedSlots: { customRender: 'matnrRender' },
        },
        {
          title: '物料名称',
          dataIndex: 'maktx',
          width: this.maktxWidth,
          width: '120px',
          align: 'left',
          customCell: () => {
            return {
              style: {
                width: this.maktxWidth,
                overflow: 'hidden',
                whiteSpace: 'nowrap',
                textOverflow: 'ellipsis',
                cursor: 'pointer',
              },
            }
          },
          customRender: (text, record) => (
            <a-tooltip placement="topLeft" title={record.maktx}>
              {record.maktx}
            </a-tooltip>
          ),
        },
        {
          title: '单位',
          dataIndex: 'meins',
          width: '80px',
          align: 'center',
        },
        {
          title: '领用数量',
          dataIndex: 'bdmng',
          width: '100px',
          align: 'center',
          scopedSlots: { customRender: 'bdmngRender' },
        },
        {
          title: '领用金额',
          dataIndex: 'totalPrice',
          width: '100px',
          align: 'center',
        },
        // {
        //   title: '移动单价',
        //   dataIndex: 'price',
        //   width: '100px',
        //   align: 'center',
        // },
        {
          title: '收货方',
          dataIndex: 'wempf',
          width: '180px',
          align: 'center',
        },
        // {
        //   title: '项目负责人',
        //   dataIndex: 'projectManager',
        //   width: '120px',
        //   align: 'center',
        // },
        // {
        //   title: '是否急用',
        //   dataIndex: 'isUrgent',
        //   width: '100px',
        //   align: 'center',
        // },
        {
          title: '备注',
          dataIndex: 'remark',
          width: '100px',
          align: 'center',
          scopedSlots: { customRender: 'remarkRender' },
        },
        {
          title: '可用库存',
          dataIndex: 'lessNum',
          width: '100px',
          align: 'center',
        },
        {
          title: '预留数量',
          dataIndex: 'reserveNum',
          width: '100px',
          align: 'center',
        },
        {
          title: '提货数量',
          dataIndex: 'ENMNG',
          width: '100px',
          align: 'center',
        },
        {
          title: '库存总数',
          dataIndex: 'stockNumTotal',
          width: '100px',
          align: 'center',
        },
        {
          title: '成本中心',
          dataIndex: 'kostl',
          width: '140px',
          align: 'center',
          scopedSlots: { customRender: 'kostlRender' },
        },
        {
          title: 'WBS元素',
          dataIndex: 'posid',
          width: '140px',
          align: 'center',
          scopedSlots: { customRender: 'posidRender' },
        },
        {
          title: '需求部门',
          dataIndex: 'KTEXT',
          width: '100px',
          align: 'center',
          scopedSlots: { customRender: 'applyUserDeptRender' },
        },
        {
          title: '订单下达人',
          dataIndex: 'createOrderUser',
          width: '120px',
          align: 'center',
          scopedSlots: { customRender: 'applyUserRender' },
        },
        {
          title: '预留单号',
          dataIndex: 'rsnum',
          width: '100px',
          align: 'center',
        },
        {
          title: '预留单项目号',
          dataIndex: 'rspos',
          width: '120px',
          align: 'center',
        },

        {
          title: '库存地点',
          dataIndex: 'lgort',
          width: '100px',
          align: 'center',
        },
      ],
      urls: {
        page: '/materials/item/page',
      },
      selectedRow: {},
      selectedRowKeys: [],
      selectedRow: [],
      
    }
  },
  mounted() {
    this.fetchList()
    this.fetchCodes(['MaterialsApplyStatus'])
  },
  methods: {
    handleSearch() {
      this.$set(this.searchForm, 'current', 1)
      this.showSearchForm = false
      this.fetchList()
    },
    handleExport() {
      let url = process.env.VUE_APP_API_BASE_URL + '/materials/item/export?1=1'
      this.handleExportXls('领用物料列表', url, this.searchForm)
    },
    fetchList() {
      postAction(this.urls.page, this.searchForm).then((res) => {
        if (res.success) {
          let list = res.result.records
          list.forEach((l) => {
            l.randomStr = this.getRandomStr(10)
          })
          this.records = list
          this.$set(this.searchForm, 'total', res.result.total)
        } else {
          this.$message.error('获取权限内成本中心号失败:' + res.$message)
        }
      })
    },
    onSelectChange(selectedRowKeys, selectedRow) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRow = selectedRow
    },
    customRow(record) {
      return {
        on: {
          dblclick: (event) => {
            this.currentRow = {}
            this.show = false
            this.$emit('close', record)
          },
          click: (event) => {
            this.currentRow = record
          },
        },
      }
    },
    handleOk() {
      if (this.selectedRow && this.selectedRow.length > 0) {
        this.$emit('close', this.selectedRow[0])
      }
      this.show = false
    },
    pageChange(current, pageSize) {
      this.$set(this.pagination, 'pageSize', pageSize)
      this.$set(this.pagination, 'current', current)
      this.fetchList()
    },
    onShowSizeChange(current, pageSize) {
      this.$set(this.pagination, 'pageSize', pageSize)
      this.$set(this.pagination, 'current', 1)
      this.fetchList()
    },
    getRandomStr(len) {
      len = len || 32
      var $chars =
        'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678' /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
      var maxPos = $chars.length
      var pwd = ''
      for (let i = 0; i < len; i++) {
        pwd += $chars.charAt(Math.floor(Math.random() * maxPos))
      }
      return pwd
    },
    toDetail(id) {
      let urlJump = this.$router.resolve({ path: '/materials/editConsum/' + id })
      window.open(urlJump.href, '_blank')
    },
  },
}
</script>
<style>
.custom-tree-node {
  color: red;
}
.el-dialog-div {
  height: 60vh;
  overflow-y: auto;
}
</style>
<style lang="less" scoped>
@import '~@assets/less/hte-main.less';
.page-main {
  .banner {
    background-image: url(~@assets/contract-top.png);
  }
  .container {
    .content-list {
      margin-top: 15px;
    }
  }
  .font-active {
    cursor: pointer;
  }
}
/deep/ #process-container {
  width: unset !important;
}
#process-container {
  background: #fff;
  padding: 40px 22px;
  width: auto;
  margin: 0 auto;
  -webkit-box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.15);
  box-shadow: 0 0 10px 0 rgba(0, 0, 0, 0.15);
}
.hte-table-content {
  margin-bottom: 20px;
}
</style>
